/**
 * @description 路由守卫
 * @description 1. 全局前置守卫
 * @description 2. 全局解析守卫
 * @description 3. 全局后置守卫
 */
import 'nprogress/nprogress.css'
import NProgress from 'nprogress'

// 类型
import type { Router } from 'vue-router'

// 全局前置守卫
const beforeEach = (router: Router) => {
  router.beforeEach(async (to, _from, next) => {
    // 进度条加载
    NProgress.start()

    // 设置页面标题
    if (to.meta && to.meta.title && typeof to.meta.title === 'string') {
      document.title = to.meta.title
    }

    next()
  })
}

// 全局解析守卫
const beforeResolve = (router: Router) => {
  router.beforeResolve((_to, _from, next) => {
    next()
  })
}

// 全局后置守卫
const afterEach = (router: Router) => {
  router.afterEach(() => {
    // 进度条结束
    NProgress.done()
  })
}

// 导出路由守卫
export default function Guard(router: Router) {
  beforeEach(router)
  beforeResolve(router)
  afterEach(router)
}